//
//  SearchByChooseViewController.m
//  搜索4
//
//  Created by dllo on 15/5/28.
//  Copyright (c) 2015年 韩志花. All rights reserved.
//

#import "SearchByChooseViewController.h"
#import "MyFMCategotyDetailCell.h"
#import "SANetWorkingTask.h"
#import "BookDetailController.h"
@interface SearchByChooseViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

@property(nonatomic, retain)UITableView *table;
@property(nonatomic, retain)NSMutableArray *tableArray;

@end

@implementation SearchByChooseViewController
- (void)dealloc
{
    [_table release];
    [_mySearch release];
    [_searchBarText release];
    [_tableArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

#pragma mark - 创建searchBar

- (void)createSearchBar
{
    /*
     // 键盘样式
     field1.keyboardType  = UIKeyboardTypeURL;
     
     // 键盘返回按钮的样式
     field1.returnKeyType = UIReturnKeyGoogle;

     */
    self.mySearch = [[UISearchBar alloc]initWithFrame:CGRectMake(0,  0 , [UIScreen mainScreen].bounds.size.width, 30)];
    [self.mySearch setBackgroundColor:[UIColor grayColor]];
    [self.mySearch setPlaceholder:@"输入小说名或者作者"];
    self.mySearch.delegate = self;
    self.mySearch.autocapitalizationType = UITextAutocorrectionTypeYes;
    self.mySearch.keyboardType = UIKeyboardTypeTwitter;
    self.mySearch.showsCancelButton = NO;
    [self.view addSubview:self.mySearch];
    [self.mySearch release];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.mySearch becomeFirstResponder];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.searchBarText = self.mySearch.text;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
//    NSLog(@"进行拼接的字符串是%@",self.mySearch.text);
    [self.mySearch resignFirstResponder];
    [self dataRequestSearchWithBookname:self.searchBarText];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.mySearch resignFirstResponder];
}

- (void)loadView

{
    [super loadView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"搜索页面";
    [self.view setBackgroundColor:[UIColor whiteColor]];
//    [self tableCreat];
    [self createSearchBar];
    
    // Do any additional setup after loading the view.
}


- (void)dataRequestSearchWithBookname:(NSString *)bookname
{
    
    
    NSDictionary *dic = @{@"page":@"1", @"version":@"1.3", @"bookname":bookname};
    [SANetWorkingTask request:@"http://vip.hentr.com/zhuibook/api/zhuibook/searchList.do" parmaters:dic block:^(NSDictionary *result) {
        self.tableArray = [[result objectForKey:@"response"] objectForKey:@"ReturnData"];
//        NSLog(@"self.tableArray = %@", self.tableArray);
        [self tableCreat];
        [self.table reloadData];
    }];
    
    
    
}


- (void)tableCreat
{
    
    self.navigationController.navigationBar.translucent = NO;
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 - 49) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
   
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, self.table.frame.size.width, 20)];
    label.font = [UIFont systemFontOfSize:20];
    [self.table setTableFooterView:label];
    if (self.tableArray.count > 0) {
        label.text = @"没有更多的搜索结果了";
    } else {
        label.text = @"什么也没有搜到";
    }
    [self.table release];
    
    
}


#pragma mark - table的协议实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIn = @"cell";
    MyFMCategotyDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIn];
    if (!cell) {
        cell = [[MyFMCategotyDetailCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIn];
    }
    
    // 设置点击不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 取出行所对应的数字元素
    NSMutableDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    // 给cell赋值
    [cell addDataToView:dic];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
#pragma mark -
#pragma mark - 点击cell跳转页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //跳到书籍详情页面 传id过去
    
    // 点击某行跳到相对应的section所在的视图
    // 需要传值:storyType对应的value传给下一个页面的pxId
    NSMutableDictionary *dic =  [self.tableArray objectAtIndex:indexPath.row];
    
    BookDetailController *detail = [[BookDetailController alloc]init];
    // 传值
    
    //    detail.delegate = self;
    
    detail.str = [NSString stringWithFormat:@"%@",
                  [dic objectForKey:@"bookid"]];
    
    // 影藏tabbar, 回来时就又有了
    detail.hidesBottomBarWhenPushed = NO;
    // 跳转页面
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}

//
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
